﻿Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Configuration.ConfigurationManager
Partial Class IntraReportCinco
    Inherits System.Web.UI.Page

    'instanciar la conexion
    Dim cn As New SqlConnection(ConnectionStrings("cn").ConnectionString)

    'Funcion q retorne las fechas
    Private Function fechas() As DataTable
        Dim da As New SqlDataAdapter(" Select distinct fecha_entrega from tb_orden_compra", cn)
        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    'funcion del reporte
    Private Function Reporte(ByVal f1 As Date, ByVal f2 As Date) As DataTable
        Dim da As New SqlDataAdapter("usp_ReporteOrdenesFechasEntrega", cn)
        da.SelectCommand.CommandType = CommandType.StoredProcedure
        With da.SelectCommand.Parameters
            .Add("@f1", SqlDbType.Date).Value = f1
            .Add("@f2", SqlDbType.Date).Value = f2
        End With
        Dim tb As New DataTable
        da.Fill(tb)
        Return tb
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            cboFec1.DataSource = fechas()
            cboFec1.DataTextField = "fecha_entrega"
            cboFec1.DataBind()

            cboFec2.DataSource = fechas()
            cboFec2.DataTextField = "fecha_entrega"
            cboFec2.DataBind()
        End If
    End Sub


    Protected Sub btnBuscarFechas_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnBuscarFechas.Click
        'imstancia del reporte
        Dim rp As New ReportDocument
        rp.Load(Server.MapPath("rptPedidosentreFechasEntrega.rpt"))
        rp.SetDataSource(Reporte(cboFec1.Text, cboFec2.Text))

        'mostrar
        CrystalReportViewer1.ReportSource = rp
        CrystalReportViewer1.DataBind()
    End Sub

    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        Response.Redirect("IntraReportesMenu.aspx")
    End Sub
End Class
